﻿using System;
using System.Windows;
using System.Windows.Input;
using Microsoft.Phone.Controls;
using PocketBlueBird.ViewModels;
using System.Windows.Controls;

namespace PocketBlueBird
{
    public partial class MainPanoramaPage : PhoneApplicationPage
    {
        MainPanoramaPageViewModels _mainPanoramaPageViewModels;
        long replyId;

        public bool GettingData
        {
            get
            {
                return this._waiting.Visibility == Visibility.Visible;
            }
            set
            {
                if (value)
                {
                    this._waiting.Visibility = Visibility.Visible;
                }
                else
                {
                    this._waiting.Visibility = Visibility.Collapsed;
                }
            }
        }

        public MainPanoramaPage()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (MainPanoramaPageViewModels.IsAuthorized)
            {
                //get tweets followers...

                _mainPanoramaPageViewModels = new MainPanoramaPageViewModels();

                _mainPanoramaPageViewModels.IsGettingData = isGettingData =>
                {
                    if (Application.Current.RootVisual.Dispatcher.CheckAccess())
                    {
                        this.GettingData = isGettingData;
                    }
                    else
                    {
                        this.Dispatcher.BeginInvoke(() =>
                        {
                            this.GettingData = isGettingData;
                        });
                    }
                };

                this.DataContext = _mainPanoramaPageViewModels;

                if (App.TweetsDic.Count == 0)
                {
                    _mainPanoramaPageViewModels.GetTweets(Dispatcher);
                }
                else
                {
                    _mainPanoramaPageViewModels.Tweets.Clear();

                    foreach (var status in App.TweetsDic.Values)
                    {
                        _mainPanoramaPageViewModels.Tweets.Add(status);
                    }
                }

                if (App.MyTweetsDic.Count != 0)
                {
                    _mainPanoramaPageViewModels.MyTweets.Clear();

                    foreach (var myTweets in App.MyTweetsDic.Values)
                    {
                        _mainPanoramaPageViewModels.MyTweets.Add(myTweets);
                    }
                }

                if (App.MentionsDic.Count != 0)
                {
                    _mainPanoramaPageViewModels.Mentions.Clear();

                    foreach (var mentions in App.MentionsDic.Values)
                    {
                        _mainPanoramaPageViewModels.Mentions.Add(mentions);
                    }
                }

                if (App.FollowersDic.Count != 0)
                {
                    _mainPanoramaPageViewModels.Followers.Clear();

                    foreach (var status in App.FollowersDic.Values)
                    {
                        _mainPanoramaPageViewModels.Followers.Add(status);
                    }
                }

                if (App.FriendsDic.Count != 0)
                {
                    _mainPanoramaPageViewModels.Friends.Clear();

                    foreach (var status in App.FriendsDic.Values)
                    {
                        _mainPanoramaPageViewModels.Friends.Add(status);
                    }
                }
            }
            else
            {
                showDialogBox();
            }
        }

        private void showDialogBox()
        {
            if (MessageBox.Show("Go to the Log in Page?", "Haven't Logined", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                this.NavigationService.Navigate(new Uri("/Views/LoginPage.xaml", UriKind.Relative));
            }
        }

        private void _btnRefresh_Click(object sender, EventArgs e)
        {
            if (MainPanoramaPageViewModels.IsAuthorized)
            {
                if (this.PanoramaRoot.SelectedItem is PanoramaItem)
                {
                    PanoramaItem selectedPanoramaItem = this.PanoramaRoot.SelectedItem as PanoramaItem;

                    switch (selectedPanoramaItem.Header.ToString())
                    {
                        case "Tweets":
                            _mainPanoramaPageViewModels.GetTweets(Dispatcher);
                            break;
                        case "My Tweets":
                            _mainPanoramaPageViewModels.GetMyTweets(Dispatcher);
                            break;
                        case "Mentions":
                            _mainPanoramaPageViewModels.GetMentions(Dispatcher);
                            break;
                        case "Messages":
                            _mainPanoramaPageViewModels.GetMessages(Dispatcher);
                            break;
                        case "Followers":
                            _mainPanoramaPageViewModels.GetFollowers(Dispatcher);
                            break;
                        case "Following":
                            _mainPanoramaPageViewModels.GetFollowing(Dispatcher);
                            break;
                        default:
                            break;
                    }
                }
            }
            else
            {
                showDialogBox();
            }
        }

        private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Views/LoginPage.xaml", UriKind.Relative));
        }

        private void _btnNew_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Views/ComposeNewTweetPage.xaml", UriKind.Relative));
        }

        private void Retweet_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            if (sender is HyperlinkButton)
            {
                HyperlinkButton hBtn = sender as HyperlinkButton;
                _mainPanoramaPageViewModels.Retweet(Dispatcher, long.Parse(hBtn.Tag.ToString()));
            }
            e.Complete();
            e.Handled = true;
        }

        private void Favorite_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            if (sender is HyperlinkButton)
            {
                HyperlinkButton hBtn = sender as HyperlinkButton;
                _mainPanoramaPageViewModels.FavoriteTweet(Dispatcher, long.Parse(hBtn.Tag.ToString()));
            }
            e.Complete();
            e.Handled = true;
        }

        private void btn_NavigateDetailMessagePage(object sender, ManipulationStartedEventArgs e)
        {
            if (sender is HyperlinkButton)
            {
                HyperlinkButton hBtn = sender as HyperlinkButton;
            }
        }
    }
}